﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace WpfEfDAL
{
    partial class Customer : IDataErrorInfo
    {
        public Customer()
        {
            this.LastName = "[new]";
        }
        partial void OnLastNameChanged()
        {
             this.RemoveError("LastName");

        if (_LastName == null || _LastName.Trim() == "" || _LastName == "[new]" )
        { this.AddError("LastName", "Please enter a last name."); }
        else if (! Regex.IsMatch(_LastName, "^([a-zA-Z '-]+)$")) {
            this.AddError("LastName", "Last name can only contain letters A-Z, spaces or dashes.");
        }
        }

        #region IDataErrorInfo Members

        private Dictionary<string,string>  m_validationErrors =  new Dictionary<string,string>();

        private void AddError(string columnName, string msg )
        {
            if (! m_validationErrors.ContainsKey(columnName)) {
                m_validationErrors.Add(columnName, msg);
            }
            
        }

        private void RemoveError(string columnName ) {
            if (m_validationErrors.ContainsKey(columnName)) {
                m_validationErrors.Remove(columnName);
            }
        }

        public string Error
        {
            get { 
                if (m_validationErrors.Count > 0) {
                    return "Customer data is invalid";
                } else return null;
                }
        }

        public string this[string columnName]
        {
            get { 
                if (m_validationErrors.ContainsKey(columnName)) {
                    return m_validationErrors[columnName];
                }else return null;
            }
        }

        #endregion
    }
}
